#ifndef BASE_IPGEO_H_
#define BASE_IPGEO_H_

#include <stdint.h>


#define MAX_IPSECTION_COUNT (4096 * 1024)

struct IPGeoItem {
        uint32_t ipbegin;
        uint32_t ipend;
        uint32_t isp;
        uint32_t geo;
        bool operator < (const IPGeoItem& other) const {
                if (ipend < other.ipbegin) {
                        return true;
                }
                return false;
        }

        friend bool operator == (const IPGeoItem& lhs, const IPGeoItem& rhs);
};

inline bool operator == (const IPGeoItem& lhs, const IPGeoItem& rhs)
{
        if ((lhs.ipbegin <= rhs.ipend) && (lhs.ipend >= rhs.ipbegin)) {
                return true;
        }
        return false;
}

class IPGeo
{
        public:
                IPGeo();
                ~IPGeo();

        public:
                static int Init(const char* ipfile = NULL, const char* ipfile_ext = NULL);
                static void QueryISPGeo(uint32_t ip, uint32_t &isp, uint32_t &geo);
        private:
                static IPGeoItem arrIPGeo[MAX_IPSECTION_COUNT];
                static uint32_t uiIPGeoCount;
};

#endif

